//
//  YZCommunityHomeVC.m
//  VET
//
//  Created by YZ on 2018/7/17.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZCommunityHomeVC.h"
#import "YZCommunityWebViewModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "YZCommunityMineVC.h"
#import "YZNetworkConfig.h"
#import "YZStatementHeader.h"

@interface YZCommunityHomeVC ()

@property (nonatomic, strong) YZCommunityWebViewModel *viewModel;

@property (nonatomic, strong) UIImageView *avatarImgView;

@property (nonatomic, copy) NSString *preAvatar;

@end

@implementation YZCommunityHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kColorWithHex(0xFFFFFF);
    [self configUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:NO];
    [self.navigationController setNavigationBarHidden:NO];
    
    [self updateAvatar];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)configUI
{
    self.title = @"圈子";
    self.view.backgroundColor = kColorWithHex(0xFFFFFF);
    self.avatarImgView = [UIImageView new];
    self.avatarImgView.frame = CGRectMake(0, 0, kCurrentDeviceNum(30), kCurrentDeviceNum(30));
    self.avatarImgView.layer.masksToBounds = YES;
    self.avatarImgView.layer.cornerRadius = kCurrentDeviceNum(15);
    self.avatarImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarImgClicked:)];
    [self.avatarImgView addGestureRecognizer:tap];
    UIView *avatarImgBGView = [[UIView alloc] initWithFrame:self.avatarImgView.frame];
    avatarImgBGView.userInteractionEnabled = YES;
    [avatarImgBGView addSubview:self.avatarImgView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:avatarImgBGView];
    
    UIButton *publishBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kCurrentDeviceNum(20), kCurrentDeviceNum(20))];
    [publishBtn setImage:[UIImage imageNamed:@"ico_camera24"] forState:UIControlStateNormal];
    [publishBtn addTarget:self action:@selector(publishBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:publishBtn];
    
    [self updateAvatar];
}

- (void)updateAvatar
{
    NSString *avatarUrl = [YZUser sharedInstance].avatarOriginal;
    if (self.preAvatar.length > 0 && ![self.preAvatar isEqualToString:avatarUrl]) {
        [self.webView evaluateJavaScript:@"javascript:yz.native2js({callback:\"onReload\"})" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            if (!error) {
                NSLog(@"调用成功");
            }
        }];
    }
    
    if (![self.preAvatar isEqualToString:avatarUrl]) {
        if (avatarUrl.length == 0) {
            NSLog(@"avatarUrl:头像路径为空");
        }
        [self.avatarImgView sd_setImageWithURL:[NSURL URLWithString:avatarUrl] placeholderImage:[UIImage imageNamed:@"icon_im"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            self.preAvatar = avatarUrl;
            NSLog(@"左按钮成功");
        }];
    }
}

- (void)avatarImgClicked:(UITapGestureRecognizer *)ges
{
    NSString *communityMineUrl = [NSString stringWithFormat:@"%@/#/profile?uid=%@&tab=0",[YZNetworkConfig getUrlCurrentDomain],[YZUser sharedInstance].uid];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;
    YZCommunityMineVC *mineVC = [[YZCommunityMineVC alloc] initWithUrl:communityMineUrl registerFunctions:nil];
    [self.navigationController pushViewController:mineVC animated:YES];
}

- (void)publishBtnClicked:(UIButton *)btn
{
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:nil action:nil];
    UIViewController *vc = [NSClassFromString(@"YZPublishCommunityVC") new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma  mark -- override

- (void)customUserContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
}

- (void)customWebView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
    [self.viewModel doActionWithMessage:message currentController:self];
    completionHandler();
}

- (void)customWwebView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    decisionHandler(WKNavigationActionPolicyAllow);
}

#pragma mark -- setters && getters

- (YZCommunityWebViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YZCommunityWebViewModel alloc] init];
    }
    return _viewModel;
}

@end
